<?php

class mysql
{
    protected static  $connection;
    protected static  $query_result;
    protected static  $server;
    protected static  $user;
    protected static  $password;
    protected static  $database;
    protected static  $persistency;
    protected static  $num_queryes = 0;

    public function GetNumQueryes()
    {
    	return $this->num_queryes;
    }
    
    public function GetConnection()
    {
    	return $this->connection;
    }
    
    public function GetResult()
    {
    	return $this->query_result;
    }
    
    
    public function mysql($server, $user, $password, $database = "", $persistency = true)
    {
        $this->server      = $server;
        $this->user        = $user;
        $this->password    = $password;
        $this->database    = $database;
        $this->persistency = $persistency;

        if ( $this->persistency ) {
            if ( ! ( $connection = @mysql_pconnect($this->server, $this->user, $this->password) ) ) {
                echo "<span class=\"fatal_error\">Error conection to database persistency!</span>\n";
                exit(0);
            }
        } else {
            if ( ! ( $connection = @mysql_connect($this->server, $this->user, $this->password) ) ) {
                echo "<span class=\"fatal_error\">Error conection to database!</span>\n";
                exit(0);
            }
        }

        if ( ! empty($database) ) {
            if ( ! @mysql_select_db($this->database, $connection) ) {
                echo "<span class=\"fatal_error\">Error database select!</span>\n";
                mysql_close($connection);
                exit(0);
            }
        }
        $this->connection = $connection;
    }

    public function query($query)
    {
        if ( ! empty($query) ) {
            $this->num_queryes++;
            if ( ! ( $this->query_result = @mysql_query($query, $this->connection) ) ) {
                echo "<span class=\"fatal_error\">Error query execution!</span>\n";
                exit(0);
            }
            return $this->query_result;
        } else {
            return null;
        }

    }

    public function fetchAssoc()
    {
        return @mysql_fetch_assoc($this->query_result);
    }

    public function close()
    {
        if ( $this->connection ) {
            if( $this->query_result ) {
                mysql_free_result($this->query_result);
            }
            if ( ! @mysql_close($this->connection) ) {
                echo "<span class=\"fatal_error\">Error close connection!</span>\n";
                exit(0);
            }
            return true;
        }
    }

 
    public function mod_mysql_numrows($query_id = null)
    {
        if ( empty($query_id) ) {
            $query_id = $this->query_result;
        }
        if ( $query_id ) {
            return @mysql_num_rows($query_id);
        } else {
            return null;
        }
    }
    
} 

?>